package com.example.myapplication.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.CountDownTimer;
import android.util.AttributeSet;
import android.view.View;

import com.example.myapplication.R;


public class CountDownProgressView extends View {

    private Context mContext;
    private Paint mPaint;
    private int MAX_PROGRESS = 100;
    /** * 弧度 */
    private int mAngle;

    /** * 外圆颜色 */
    private int outRoundColor;
    /** * 内圆的颜色 */
    private int inRoundColor;
    /** * 线的宽度 */
    private int roundWidth;

    /** * 进度条颜色 */
    private int progressBarColor;


    private CountDownTimer timer ;


    public CountDownProgressView(Context context) {
        this(context, null);
    }
    public CountDownProgressView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public CountDownProgressView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr);
        mContext = context;
        init(attrs);
    }

    @TargetApi(21)
    public CountDownProgressView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mContext = context;
        init(attrs);
    }

    public void init(AttributeSet attrs) {
        mPaint = new Paint();
        TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.CircleProgressBar);
        outRoundColor = typedArray.getColor(R.styleable.CircleProgressBar_outCircleColor, getResources().getColor(R.color.transparent));
        inRoundColor = typedArray.getColor(R.styleable.CircleProgressBar_inCircleColor, getResources().getColor(R.color.transparent));
        progressBarColor = typedArray.getColor(R.styleable.CircleProgressBar_progressColor, getResources().getColor(R.color.transparent));
        roundWidth = typedArray.getDimensionPixelOffset(R.styleable.CircleProgressBar_lineWidth, 3);
        MAX_PROGRESS = typedArray.getInt(R.styleable.CircleProgressBar_maxProgress,100);
        typedArray.recycle();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int center = getWidth() / 2;
        int radius = (center - roundWidth / 2);

        mPaint.setColor(outRoundColor);
        mPaint.setStrokeWidth(roundWidth);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAntiAlias(true);
        canvas.drawCircle(center, center, radius, mPaint);

        mPaint.setColor(inRoundColor);
        radius = radius - roundWidth;
        canvas.drawCircle(center, center, radius, mPaint);
//        radius = radius+roundWidth;
        mPaint.setColor(progressBarColor);
        RectF rectF = new RectF(center - radius, center - radius, center + radius, center + radius);
        canvas.drawArc(rectF, -90, mAngle, false, mPaint);

        canvas.save();

//        mPaint.setStyle(Paint.Style.FILL);
//        canvas.translate(center, center);
//        canvas.rotate(mAngle - 90);
//        canvas.translate(radius, 0);
//        canvas.drawCircle(0, 0, roundWidth, mPaint);

        canvas.restore();
    }

    public void startCountDown(long countDownTime){
        if(timer!=null){
            timer.cancel();
        }
        float steap = (float) MAX_PROGRESS/countDownTime;
        timer = new CountDownTimer(countDownTime, 16) {
            @Override
            public void onTick(long l) {
                float   degree = (countDownTime-l) * steap;
                mAngle = (int) (360 * degree / MAX_PROGRESS);
                invalidate();
            }

            @Override
            public void onFinish() {
                setVisibility(GONE);
            }
        };
        timer.start();
    }
    public void cancel(){
        timer.cancel();
        setVisibility(GONE);
    }
}